package com.ops.service.ns.system.beans;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;

import com.ops.schema.ns.system.OPSLookUps;
import com.ops.schema.ns.system.OPSMessages;
import com.ops.schema.ns.system.OPSRecordAuditInfo;
import com.ops.schema.ns.system.SearchLookUpsRequest;
import com.ops.schema.ns.system.SearchLookUpsResponse;
import com.ops.schema.ns.system.SearchMessagesRequest;
import com.ops.schema.ns.system.SearchMessagesResponse;
import com.ops.schema.ns.system.UpdateMessagesRequest;
import com.ops.schema.ns.system.UpdateMessagesResponse;
import com.ops.service.ns.system.client.OnlinePayrollSystemServiceClient;
import com.ops.service.ns.system.exception.ServiceException;

public class MessageMBean {

	private String msgName;
	private String msgLang;
	private String activeFrom;
	private String activeTo;
	private String mode;
	private String delflag;
	private String saveStatus;
	private List<OPSLookUps> oMaster;
	private List<SelectItem> langList;
	private List<OPSMessages> opsMessages;
	private OPSMessages opsMsgs;
	private Map<String, OPSMessages> opsMsgMap;

	public void searchMessages() {
		System.out.println("Inside searchMessages");
		SearchMessagesRequest msgRequest = new SearchMessagesRequest();
		msgRequest.setName(msgName);
		msgRequest.setLang(msgLang);
		msgRequest.setActiveFrom(activeFrom);
		msgRequest.setActiveTo(activeTo);
		OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
		uditInfo.setUpdatedBy(getuserId());
		uditInfo.setUpdatedSession(getSessionId());
		uditInfo.setAction("S");
		uditInfo.setObjectType(getMenuId());
		msgRequest.setOPSRecordAuditInfo(uditInfo);
		try {
			SearchMessagesResponse messagesResponse = OnlinePayrollSystemServiceClient
					.searchMessage(msgRequest);
			setOpsMessages(messagesResponse.getOPSMessages());

		} catch (ServiceException e) {
			e.printStackTrace();
		}

	}

	public void resetMessages() {
		setMsgName(null);
		setMsgLang(null);
		setActiveFrom(null);
		setActiveTo(null);
	}
	
	public void saveMessages(ActionEvent event) {

		UpdateMessagesRequest updateRequest = new UpdateMessagesRequest();
		OPSMessages opsMessages = new OPSMessages();
		opsMessages.setLang(opsMsgs.getLang());
		opsMessages.setName(opsMsgs.getName());
		opsMessages.setKey(opsMsgs.getKey());
		opsMessages.setId(opsMsgs.getId());
		opsMessages.setValue(opsMsgs.getValue());
		opsMessages.setActiveFrom(opsMsgs.getActiveFrom());
		opsMessages.setActiveTo(opsMsgs.getActiveTo());
		opsMessages.setOperationFlag("U");
		OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
		uditInfo.setUpdatedBy(getuserId());
		uditInfo.setUpdatedSession(getSessionId());
		if(opsMessages.getId() != null)
			uditInfo.setAction("U");
			else
			uditInfo.setAction("I");	
			uditInfo.setObjectType(getMenuId());
		opsMessages.setOPSRecordAuditInfo(uditInfo);
		updateRequest.getOPSMessages().add(opsMessages);
		try {
			UpdateMessagesResponse response=persistData(updateRequest);
			setSaveStatus(response.getServiceResult().getStatusMsg());
			searchMessages();
		} catch (ServiceException e) {
			e.printStackTrace();
		}

	}

	public void delMessages(ActionEvent event) {

		if (mode != null && !mode.equalsIgnoreCase("")) {
			if (mode.startsWith("Del_") && delflag != null
					&& delflag.equalsIgnoreCase("Yes")) {
				setDelflag(null);
				UpdateMessagesRequest updateRequest = new UpdateMessagesRequest();
				String key = mode.replace("Del_", "").trim();
				OPSMessages messages = new OPSMessages();
				messages.setId(key);
				messages.setOperationFlag("D");
				OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
				uditInfo.setUpdatedBy(getuserId());
				uditInfo.setUpdatedSession(getSessionId());
				uditInfo.setAction("D");	
				uditInfo.setObjectType(getMenuId());
				messages.setOPSRecordAuditInfo(uditInfo);
				updateRequest.getOPSMessages().add(messages);
				try {
					persistData(updateRequest);
					searchMessages();
				} catch (ServiceException e) {
					e.printStackTrace();
				}

			}
		}

	
	}

	public OPSMessages addMessages() {
		setSaveStatus(null);
		if (mode != null && !mode.equalsIgnoreCase("")) {
			if (getOpsMessages() != null) {
				opsMsgMap = new HashMap<String, OPSMessages>();
				for (OPSMessages Messages : getOpsMessages()) {
					System.out.println("Inside" + Messages.getId());
					opsMsgMap.put(Messages.getId(), Messages);
				}
			}
			if (mode.equalsIgnoreCase("newAdd")) {
				opsMsgs = new OPSMessages();
			} else if (mode.startsWith("Edit_")) {
				String key = mode.replace("Edit_", "").trim();
				System.out.println("Key:" + key);
				opsMsgs = opsMsgMap.get(key);
			}
		}
		System.out.println("Inside addMessages:::" + mode);
		return opsMsgs;
	}

	
	
	
	public UpdateMessagesResponse persistData(
			UpdateMessagesRequest messagesRequest) throws ServiceException {
		UpdateMessagesResponse messagesResponse = OnlinePayrollSystemServiceClient
				.saveMessage(messagesRequest);
		return messagesResponse;
	}

	private String getSessionId() {

		FacesContext fCtx = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) fCtx.getExternalContext().getSession(false);
		return session.getId();
	}
	private String getuserId() {

		ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
		Map<String, Object> sessionMap = externalContext.getSessionMap();
		return sessionMap.get("UserId").toString();
	}
	
	private String getMenuId() {

		ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
		Map<String, Object> sessionMap = externalContext.getSessionMap();
		return sessionMap.get("MenuId").toString();
	}
	
	public String getMsgName() {
		return msgName;
	}

	public void setMsgName(String msgName) {
		this.msgName = msgName;
	}

	public String getMsgLang() {
		return msgLang;
	}

	public void setMsgLang(String msgLang) {
		this.msgLang = msgLang;
	}

	public String getActiveFrom() {
		return activeFrom;
	}

	public void setActiveFrom(String activeFrom) {
		this.activeFrom = activeFrom;
	}

	public String getActiveTo() {
		return activeTo;
	}

	public void setActiveTo(String activeTo) {
		this.activeTo = activeTo;
	}

	public String getMode() {
		return mode;
	}

	public void setMode(String mode) {
		this.mode = mode;
	}

	public List<OPSLookUps> getoMaster() {

		if (oMaster == null) {
			oMaster = new ArrayList<OPSLookUps>();
			SearchLookUpsRequest masterRequest = new SearchLookUpsRequest();
			OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
			masterRequest.setOPSRecordAuditInfo(uditInfo);
			masterRequest.setType("LANGUAGE");
			OPSLookUps oMasterObj = new OPSLookUps();
			oMasterObj.setCode("");
			oMasterObj.setName("--Select--");
			oMaster.add(oMasterObj);
			try {
				SearchLookUpsResponse masterResponse = OnlinePayrollSystemServiceClient
						.searchLookUp(masterRequest);
				for (OPSLookUps OPSLookUps : masterResponse.getOPSLookUps()) {
					OPSLookUps msgMaster = new OPSLookUps();
					msgMaster.setCode(OPSLookUps.getCode());
					msgMaster.setName(OPSLookUps
							.getName());
					oMaster.add(msgMaster);
				}

			} catch (ServiceException e) {
				e.printStackTrace();
			}

		}
		return oMaster;
	}

	public void setoMaster(List<OPSLookUps> oMaster) {
		this.oMaster = oMaster;
	}

	public List<SelectItem> getLangList() {
		if (langList == null) {
			langList = new ArrayList<SelectItem>();
			List<OPSLookUps> masters = getoMaster();
			for (OPSLookUps master : masters) {
				SelectItem item = new SelectItem(master.getCode(),
						master.getName());
				langList.add(item);
			}

		}
		return langList;
	}

	public void setLangList(List<SelectItem> langList) {
		this.langList = langList;
	}

	public List<OPSMessages> getOpsMessages() {
		return opsMessages;
	}

	public void setOpsMessages(List<OPSMessages> opsMessages) {
		this.opsMessages = opsMessages;
	}

	public OPSMessages getOpsMsgs() {
		return opsMsgs;
	}

	public void setOpsMsgs(OPSMessages opsMsgs) {
		this.opsMsgs = opsMsgs;
	}

	public Map<String, OPSMessages> getOpsMsgMap() {
		return opsMsgMap;
	}

	public void setOpsMsgMap(Map<String, OPSMessages> opsMsgMap) {
		this.opsMsgMap = opsMsgMap;
	}
	public String getDelflag() {
		return delflag;
	}

	public void setDelflag(String delflag) {
		this.delflag = delflag;
	}

	public String getSaveStatus() {
		return saveStatus;
	}

	public void setSaveStatus(String saveStatus) {
		this.saveStatus = saveStatus;
	}
}
